﻿
//以下变量为JS公共变量
var MxDrawXCtrl_Obj; //控件对象


function line()
{
	MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
	
	
	// 创建一个控件应用对象。
	var app ;
	app = new ActiveXObject("MxDrawX.MxDrawApplication");
	
	// curSpace 得到当前控件的模形空间 MxDrawX.MxDrawBlockTableRecord
    var curSpace = app.WorkingDatabase().CurrentSpace();
    var point1;
	var point2;

    // 定义直线的两个点。
    /*
	point1 = new ActiveXObject("MxDrawX.MxDrawPoint");
	point2 = new ActiveXObject("MxDrawX.MxDrawPoint");
	point1.x = 10;
	point1.y = 10;
	point1.z = 0;

	point2.x = 200;
	point2.y = 4000;
	point2.z = 0;
	*/
	
	MxDrawXCtrl_Obj.focus();
	
	
	var point1 = MxDrawXCtrl_Obj.GetPoint(null,"\n 点取第一点:");
	if(point1 == null)
	{
	    alert("用户取消..");
	    return;
	}

        var point2 = MxDrawXCtrl_Obj.GetPoint(point1,"\n 点取第二点:");
	if(point2 == null)
	{
	    alert("用户取消..");
	    return;
	}
	
        // 向当前空间增加直线 newline 是增加的新直线。
	var newline = curSpace.AddLine(point1, point2);
	
	var color = new ActiveXObject("MxDrawX.MxDrawMcCmColor");
	color.SetRGB(255,77,0);
	newline.TrueColor = color;
	
	// 把所以图形内容放到当前显示视区中.
	//app.ZoomAll();
	//alert ("绘制直线成功");
}

function MxDrawX_ImplementCommandEvent(iCmd)
{

}

function getcurpath()
{
	var   url=window.location.href; 
	var   pos=url.lastIndexOf("\\");         
  	if   (pos==-1)
  		pos=url.lastIndexOf("/");
  	var   path=url.substring(0,pos);

        var sHttp = "http://";
        var pos2 = path.indexOf(sHttp);
        var path2;
        if(pos2 == -1)
        {
		path2 = path;
        }
	else
	{
		path2 = path.substring(pos2 + sHttp.length);
	}

  	return path2;
}


function savedwgurl()
{
	
	MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
        MxDrawXCtrl_Obj.focus();
	MxDrawXCtrl_Obj.SaveDwgToURL(getcurpath(),"/saveannouce_upfile.asp","file1","80");
        var app ;
	app = new ActiveXObject("MxDrawX.MxDrawApplication");
        alert(app.GetLastError() );

}

function CreateLayer()
{
    MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
    var layerTable = MxDrawXCtrl_Obj.GetDatabase().GetLayerTable();
    
    // 新创建的层名.        
    var sNewLayerName = "NewLayer";
     
    // 向当前控件增加该层。
    var layerTableRec = layerTable.Add(sNewLayerName);
    
    // 设置层的颜色.
    var color = new ActiveXObject("MxDrawX.MxDrawMcCmColor");
	color.SetRGB(0,255,0);
	layerTableRec.Color = color;
    
    // 把新建的层设置成当前层,设置成当前层后，后面向数据库增加的实体的图层就默认该层。
    MxDrawXCtrl_Obj.GetDatabase().CurrentlyLayerName = sNewLayerName;

}

function HideLayer()
{
     MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
     var ent = MxDrawXCtrl_Obj.GetEntity("\n 选择要关闭图层的实体:");
     if (ent == null)
     {
         return;
     }
     
     var sLayerName = ent.Layer;
     var layerTable = MxDrawXCtrl_Obj.GetDatabase().GetLayerTable();
     var layerTableRec = layerTable.GetAt(sLayerName, false);
     if(layerTableRec != null)
        layerTableRec.IsOff = true;
}

function OpenAllLayer()
{
    MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
    var layerTable = MxDrawXCtrl_Obj.GetDatabase().GetLayerTable();
    var iter = layerTable.NewIterator();
    if (iter == null)
            return;

    for(;!iter.Done();iter.Step(true,false) )
    {
         var layerTableRec = iter.GetRecord();
         layerTableRec.IsOff = false;
    }
}

function OpenDwgFile()
{
     MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
   
     var openDlg = new ActiveXObject('MSComDlg.CommonDialog');
     var isOpen  = false;
     try
     {
        openDlg.Filter="dwg 文件(*.dwg)|*.dwg";
        openDlg.FilterIndex = 1;
        openDlg.MaxFileSize = 255;
        openDlg.ShowOpen();
      }
      catch(e)
      {
        alert(e.message);
        return;
      }
     if(openDlg.filename != "")
        MxDrawXCtrl_Obj.OpenDwgFile(openDlg.filename);    
}

function OpenBmpJpgFile()
{
     MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
   
     var openDlg = new ActiveXObject('MSComDlg.CommonDialog');
     var isOpen  = false;
     try
     {
        openDlg.Filter="jpg 文件(*.jpg)|*.jpg";
        openDlg.FilterIndex = 1;
        openDlg.MaxFileSize = 255;
        openDlg.ShowOpen();
      }
      catch(e)
      {
        alert(e.message);
        return;
      }
     if(openDlg.filename != "")
        MxDrawXCtrl_Obj.OpenDwgFile(openDlg.filename);     
     
}

function InsertImage()
{
     MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
   
     var openDlg = new ActiveXObject('MSComDlg.CommonDialog');
     try
     {
        openDlg.Filter="jpg 文件(*.jpg)|*.jpg";
        openDlg.FilterIndex = 1;
        openDlg.MaxFileSize = 255;
        openDlg.ShowOpen();
     }
     catch(e)
     {
        alert(e.message);
        return;
     }
     if(openDlg.filename == "")
        return;
        
     var filename = openDlg.filename;
     // 图片文件的插入点。
     var point = MxDrawXCtrl_Obj.GetPoint(null, "\n 指定插入点:");
     if (point == null)
        return;
     
     var    database = MxDrawXCtrl_Obj.GetDatabase();
     var    dict     = database.GetNamedObjectsDictionary();
     if (dict == null)
        return;
     
     var imageDict = dict.GetAt("ACAD_IMAGE_DICT", false) ;
     if(imageDict == null)
     {
          // 增加一个字典对象。
          imageDict =dict.AddObject("ACAD_IMAGE_DICT", "McDbDictionary");
     }

     if (imageDict == null)
          return;
     
     var imgedef = imageDict.GetAt(filename , false);
     if(imgedef == null)
     {
          imgedef = imageDict.AddObject(filename , "McDbRasterImageDef");
          if (imgedef == null)
              return; 
          
          imgedef.SourceFileName = filename;
     }
     
     
     var curSpace = database.CurrentSpace();
     var scale = 1.0;
     curSpace.AddImage2(point,scale, imgedef.ObjectID);

     var app ;
	 app = new ActiveXObject("MxDrawX.MxDrawApplication");
     app.ZoomAll();
}

function ZoomWindow()
{
        MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
        var point1 = MxDrawXCtrl_Obj.GetPoint(null,"\n 点取缩放区域的第一点:");
	    if(point1 == null)
	    {
	        alert("用户取消..");
	        return;
	    }

        var point2 = MxDrawXCtrl_Obj.GetPoint(point1,"\n 点取缩放区域的第二点:");
	    if(point2 == null)
	    {
	        alert("用户取消..");
	        return;
	    }
        var app ;
	    app = new ActiveXObject("MxDrawX.MxDrawApplication");
        app.ZoomWindow(point1, point2);
}


function MSComDlg_CommonDialog(cmd)
{
    if(!ActiveXObject)return;
    var obj=new ActiveXObject('MSComDlg.CommonDialog');
    var filename=false;
    try{
        switch(cmd){
        case 2:
            obj.ShowColor();
            break;
        case 3:
            obj.ShowFont();
            break;
        case 4:
            obj.ShowHelp();
            break;
        default:
            obj.Filter='所有文件(*.jpg)';
            obj.FilterIndex = 1;
            obj.MaxFileSize = 255;
            if(cmd==0)
            {
                obj.ShowSave();
            }
            else
            {
                obj.ShowOpen();
            }
        }
        filename = obj.filename;
    }catch(e){alert(e.message);}
    return filename;
} 


// 创建一个图层，并把该图层设置成当前图层的例程。
function CreateLayer()
{
     var sNewLayerName = "TestNewLayer";
     var app;
     app = new ActiveXObject("MxDrawX.MxDrawApplication");
     var layerTable = app.WorkingDatabase().GetLayerTable();

     // 
     var layerTableRec = layerTable.Add(sNewLayerName);

     // 设置图层的颜色。
     var color = new ActiveXObject("MxDrawX.MxDrawMcCmColor");
	 color.SetRGB(255,0,0);
     layerTableRec.Color = color;

     // 把新建的层设置成当前层
     app.WorkingDatabase().CurrentlyLayerName = sNewLayerName;
}

// 插入一个注释
function InsertNote()
{
     CreateLayer();
     
     var sNoteText = "这是一个注释";
     var sNote = "NoteBlockName";

     // 让用点取一个注释插入点。
     MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
     
     var point = MxDrawXCtrl_Obj.GetPoint(null,"\n 点取插入点 :");
     if (point == null)
     {
         alert("用户取消..");
         return;
     }
     
     // 插入注释。
     var app;
     app = new ActiveXObject("MxDrawX.MxDrawApplication");
     
     var curDatabase = app.WorkingDatabase();
     var blkTable = curDatabase.GetBlockTable();
     var curSpace = curDatabase.CurrentSpace();

     var blkRec = blkTable.GetAt(sNote,true);

     
     if(blkRec == null)
     {
         // blkRec == null 指名的块记录没有找到.

         // 插入一个新的块。
         var sBlkFilePath = MxDrawXCtrl_Obj.GetOcxAppPath() + "\\Blk\\TsgNote.dwg";
        

         // 把dwg文件导入到当前数据库。
         MxDrawXCtrl_Obj.InsertBlock(sBlkFilePath, sNote);

         blkRec = blkTable.GetAt(sNote,true);
     }

     if(blkRec != null)
     {
         // 在当前空间中插入一个块引用。
         curSpace.InsertBlock(point,sNote,4.0,0.0);
     }

     // 插入注释文本。
     point.y = point.y - 10.0;
     curSpace.AddText(point, sNoteText, 8, 0.0, 1,
         3);

    //curSpace.AddText(point, sNoteText, 16, 0.0, MCAD_McHorizontalAlignment.mcHorizontalAlignmentCenter,
    //            MCAD_McVerticalAlignment.mcVerticalAlignmentTop);
            
}


function WriteXData()
{
    MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
    var ent = MxDrawXCtrl_Obj.GetEntity("\n 选择写扩展数据的实体:");
    if (ent == null)
    {
         return;
    }
    
    var xData;
    xData = new ActiveXObject("MxDrawX.MxDrawResbuf");
    xData.AddString("TestApp",1001);
    xData.AddLong(77677,1071);

    ent.SetXData(xData);
}

function ReadXData()
{
        MxDrawXCtrl_Obj = document.all.item("MxDrawXCtrl");
        var ent = MxDrawXCtrl_Obj.GetEntity("\n 选择读取扩展数据的实体:");
        if (ent == null)
        {
            return;
        }
        var xData = ent.GetXData("");
        if(xData.Count == 0)
        {
            alert("没有扩展数据");
        }
        else
        {
            // 向命令行窗口打印扩展数据。
            xData.PrintData();
        }
}